{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''Extract/remove audio from video, add audio to video, and convert audio formats with FFmpeg (4.1.x).\n",
    "\n",
    "Author: He Zhang @ University of Exeter\n",
    "Date: 16th April 2019 (Update: 18th April 2019)\n",
    "Contact: hz298@exeter.ac.uk zhangheupc@126.com\n",
    "\n",
    "Copyright (c) 2019 He Zhang\n",
    "'''\n",
    "\n",
    "# Python 3.7\n",
    "\n",
    "# Install FFmpeg on Linux:\n",
    "#     $ sudo apt-get install ffmpeg\n",
    "\n",
    "# Note:\n",
    "#     Return 0 in ipynb - If the VA processing is completed.\n",
    "#     Return 1 in ipynb - If the VA processing is failed (e.g., the output file already exists).\n",
    "\n",
    "import os\n",
    "import subprocess\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1. Extract audio from video.\n",
    "\n",
    "# Set the path of input and output files.\n",
    "INPUT_VIDEO = \"Media/Demo_360P.mp4\"\n",
    "OUTPUT_FILE = \"Media/Demo.wav\"\n",
    "\n",
    "# Check if the output file exists. If so, delete it.\n",
    "if os.path.isfile(OUTPUT_FILE) is True:\n",
    "    os.remove(OUTPUT_FILE)\n",
    "\n",
    "# Set the command for processing the input video/audio.\n",
    "cmd = \"ffmpeg -i \" + INPUT_VIDEO + \" -ab 160k -ac 2 -ar 44100 -vn \" + OUTPUT_FILE\n",
    "\n",
    "# Execute the (Terminal) command within Python.\n",
    "subprocess.call(cmd, shell=True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2. Add audio to video.\n",
    "\n",
    "# Note:\n",
    "#     The input video should contain no audio.\n",
    "#     If it contains audio, new audio will not be added.\n",
    "\n",
    "# Set the path of input and output files.\n",
    "INPUT_VIDEO = \"Media/Demo_1080P.mp4\"\n",
    "INPUT_AUDIO = \"Media/Demo.wav\"\n",
    "OUTPUT_FILE = \"Media/Demo_1080P_S.mp4\"\n",
    "\n",
    "# Check if the output file exists. If so, delete it.\n",
    "if os.path.isfile(OUTPUT_FILE) is True:\n",
    "    os.remove(OUTPUT_FILE)\n",
    "\n",
    "# Set the command for processing the input video/audio.\n",
    "cmd = \"ffmpeg -i \" + INPUT_VIDEO + \" -i \" + INPUT_AUDIO + \" -c:v copy -c:a aac -strict experimental \" + OUTPUT_FILE\n",
    "\n",
    "# Execute the (Terminal) command within Python.\n",
    "subprocess.call(cmd, shell=True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3. Remove audio from video.\n",
    "\n",
    "# Set the path of input and output files.\n",
    "INPUT_VIDEO = \"Media/Demo_360P.mp4\"\n",
    "OUTPUT_FILE = \"Media/Demo_360P_noS.mp4\"\n",
    "\n",
    "# Check if the output file exists. If so, delete it.\n",
    "if os.path.isfile(OUTPUT_FILE) is True:\n",
    "    os.remove(OUTPUT_FILE)\n",
    "\n",
    "# Set the command for processing the input video/audio.\n",
    "cmd = \"ffmpeg -y -i \" + INPUT_VIDEO + \" -an -vcodec copy \" + OUTPUT_FILE\n",
    "\n",
    "# Execute the (Terminal) command within Python.\n",
    "subprocess.call(cmd, shell=True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 4. Convert audio formats.\n",
    "\n",
    "# Set the path of input and output files.\n",
    "INPUT_AUDIO = \"Media/Demo.wav\"\n",
    "OUTPUT_FILE = \"Media/Demo.mp3\"\n",
    "\n",
    "# Check if the output file exists. If so, delete it.\n",
    "if os.path.isfile(OUTPUT_FILE) is True:\n",
    "    os.remove(OUTPUT_FILE)\n",
    "\n",
    "# Set the command for processing the input video/audio.\n",
    "cmd = \"ffmpeg -i \" + INPUT_AUDIO + \" -ab 160k -ac 2 -ar 44100 -vn \" + OUTPUT_FILE\n",
    "\n",
    "# Execute the (Terminal) command within Python.\n",
    "subprocess.call(cmd, shell=True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
